package dhm.com.hpx.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.CollectUserAdapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.CollectUserBean;
import dhm.com.hpx.utils.SpUtils;

public class UserActivity extends BaseActivity implements LoginContract.IView {


    @BindView(R.id.recy)
    XRecyclerView recy;

    private PressenterImpl pressenter;
    private String uid;
    private int page=1;
    private CollectUserAdapter adapter;
    private List<CollectUserBean.DataBean> beanList;

    @Override
    protected int getLayout() {
        return R.layout.activity_user;
    }


    @Override
    protected void initView() {
        ButterKnife.bind(this);

        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        uid = SpUtils.getString(this, "uid");

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
        recy.setPullRefreshEnabled(true);
        recy.setLoadingMoreEnabled(true);
        adapter = new CollectUserAdapter(this);
        recy.setAdapter(adapter);

        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                getdata();
            }

            @Override
            public void onLoadMore() {
                getdata();
            }
        });

        getdata();

    }

    private void getdata() {
        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("page",page+"");
        pressenter.sendMessage(this,Constant.listUserQuery,map, CollectUserBean.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void requesta(Object data) {

        if (data instanceof CollectUserBean){
            CollectUserBean collectUserBean= (CollectUserBean) data;
            if (collectUserBean.getCode()==1){
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    beanList = collectUserBean.getData();
                }else {
                    beanList.addAll(collectUserBean.getData());
                }

                for (int i = 0; i < beanList.size(); i++) {
                    if (beanList.get(i).getUserId()==0){
                        beanList.remove(i);
                        i--;
                    }
                }

                adapter.setList(beanList);
                page++;
            }else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(this, collectUserBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
            recy.refreshComplete();
            recy.loadMoreComplete();
        }

    }

    @Override
    public void fail(String error) {

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
